<?php
include './include/common.inc.php';
include '../admin/include/ipbanned.class.php';
$ipbanned = new ipbanned();
$action = isset($action) ? $action : 'manage';
$forward = get_forward('/admin/ipbanned.php');
switch ($action)
{
	case 'del':
		if(check_perms('ip003')===false)
		{
			$response->alert('您没有权限进行此操作！');
			$response->execute();
		}
		if(empty($ip))
		{
			$response->alert('请选择要删除的ip');
			$response->execute();
		}
		$ip = urldecode($ip);
		$res = $ipbanned->delete_ip($ip);
		if($res === false)
		{
			$response->alert('删除失败');
		}
		else 
		{
			$response->alert('删除成功');
			$response->script('self.location=self.location');
		}
		$response->execute();
		break;
	case 'delall':
		if(check_perms('ip003')===false)
		{
			$response->alert('您没有权限进行此操作！');
			$response->execute();
		}
		if(is_array($ck) && sizeof($ck) > 0)
		{
			foreach ($ck as $ip)
			{
				$res = $ipbanned->delete_ip($ip);
			}
			$response->alert('删除成功');
			$response->script('self.location=self.location');
		}
		else 
		{
			$response->alert('请选择要删除的ip');
		}
		$response->execute();
		break;
	case 'add':
		if(check_perms('ip002')===false)
		{
			$response->alert('您没有权限进行此操作！');
			$response->execute();
		}
		if(isset($_POST['ip']))
		{
			if(empty($ip))
			{
				$response->alert('ip地址不能为空！');
				$response->execute();
			}
			$res = $ipbanned->add_ip($ip,$expires);
			if($res === false)
			{
				$response->alert('添加失败或ip地址重复');
			}
			else 
			{
				$response->alert('添加成功');
				$response->script('self.location=self.location');
			}
			$response->execute();
		}
		break;
	case 'flush':
		if(check_perms('ip003')===false)
		{
			$response->alert('您没有权限进行此操作！');
			$response->execute();
		}
		$res = $ipbanned->delete_expires();
		if($res === false)
		{
			$response->alert('清空失败！');
		}
		else 
		{
			$response->alert('清空成功！');
			$response->script('self.location=self.location');
		}
		$response->execute();
		break;
	case 'search':
		$url = $self_url;
		$search_params = array();
		if(isset($ip) && !empty($ip))
		{
			$search_params[] = 'ip='.urlencode($ip);
		}
		$pos = sizeof($search_params)>0 ? '?' : '';
		$response->redirect($url.$pos.implode('&',$search_params));
		$response->execute();
		break;
	case 'manage':
	default:
		if(check_perms('ip001')===false)
		{
			showmessage('您没有权限进行此操作！');
		}
		$ip = isset($ip) ? urldecode($ip) : '';
		$page = isset($page) ? $page : 1;
		$page = max(1,intval($page));
		$pagesize = 50;
		$list = $ipbanned->get_ips($page, $pagesize, $ip);
		$pages = $ipbanned->pages;
		include template('admin', 'ip_manage');
}
?>